home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / mimelib / token.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-14  |  4.8 KB  |  151 lines

  1. //=============================================================================
  2. // File:       token.h
  3. // Contents:   Declarations for DwTokenizer, DwRfc822Tokenizer
  4. // Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>
  5. // WWW:        http://www.fwb.gulf.net/~dwsauder/mimepp.html
  6. //
  7. // Copyright (c) 1996, 1997 Douglas W. Sauder
  8. // All rights reserved.
  9. //
  10. // IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
  11. // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
  12. // THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
  13. // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. //
  15. // DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
  16. // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  17. // PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
  18. // BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
  19. // SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. //
  21. //=============================================================================
  22.  
  23. #ifndef DW_TOKEN_H
  24. #define DW_TOKEN_H
  25.  
  26. #ifndef DW_CONFIG_H
  27. #include <mimelib/config.h>
  28. #endif
  29.  
  30. #ifndef DW_STRING_H
  31. #include <mimelib/string.h>
  32. #endif
  33.  
  34. // RFC 822 and RFC 1521 define slightly different grammars for the field
  35. // bodies they define.  The differences are that RFC 822 defines a basic
  36. // type 'atom' while RFC 1521 defines a basic type 'token', and RFC 822
  37. // defines a character class 'special' while RFC 1521 defines a character
  38. // class 'tspecial'. For this reason, we have two tokenizer classes:
  39. // Rfc822Tokenizer and Rfc1521Tokenizer. Since the basic types
  40. // quoted string, comment, and domain literal are common to both RFC 822
  41. // and RFC 1521, the common code of both tokenizer classes is
  42. // combined into a Tokenizer base class. The Tokenizer class has no public
  43. // constructors, since only objects of class Rfc822Tokenizer or
  44. // Rfc1521Tokenizer will ever be instantiated.
  45. //
  46. // Note that we do not use polymorphism here: Tokenizer has no virtual
  47. // functions. We do this for efficiency, since there is some overhead
  48. // involved with virtual functions. If the classes were more complicated
  49. // than they currently are, then virtual functions would be justified in
  50. // order to reduce the duplication of code. As it stands, though, the
  51. // classes are fairly simple and efficient.
  52. // In addition, polymorphism is not needed to use the tokenizer classes.
  53.  
  54. #if !(defined(__DECCXX) && defined(__linux__))
  55. #include <iosfwd>
  56. #endif
  57.  
  58. enum {
  59.     eTkError=-1,
  60.     eTkNull=0,
  61.     eTkSpecial,
  62.     eTkAtom,
  63.     eTkComment,
  64.     eTkQuotedString,
  65.     eTkDomainLiteral,
  66.     eTkTspecial,
  67.     eTkToken
  68. };
  69.  
  70.  
  71. class DW_EXPORT DwTokenizer {
  72.     friend class DwTokenString;
  73. public:
  74.     const DwString& Token() const { return mToken; }
  75.     int Type() const              { return mTkType; }
  76.     void StripDelimiters();
  77.     static std::ostream* mDebugOut;
  78. protected:
  79.     DwTokenizer(const DwString& aStr);
  80.     DwTokenizer(const char* aCStr);
  81.     virtual ~DwTokenizer();
  82.     void PrintToken(std::ostream*);
  83.     // Quoted strings, comments, and domain literals are parsed
  84.     // identically in RFC822 and RFC1521
  85.     void ParseQuotedString();
  86.     void ParseComment();
  87.     void ParseDomainLiteral();
  88.     // Data members
  89.     const DwString mString;
  90.     DwString mToken;
  91.     size_t mTokenStart;
  92.     size_t mTokenLength;
  93.     size_t mNextStart;
  94.     int mTkType;
  95. };
  96.  
  97.  
  98. class DW_EXPORT DwRfc822Tokenizer : public DwTokenizer {
  99.     friend class DwAddressParser;
  100. public:
  101.     DwRfc822Tokenizer(const DwString& aStr);
  102.     DwRfc822Tokenizer(const char* aCStr);
  103.     virtual ~DwRfc822Tokenizer();
  104.     int Restart();
  105.     int operator ++ (); // prefix increment operator
  106. private:
  107.     DwRfc822Tokenizer();
  108.     DwRfc822Tokenizer(const DwRfc822Tokenizer&);
  109.     void ParseToken();
  110.     void ParseAtom();
  111. };
  112.  
  113.  
  114. class DW_EXPORT DwRfc1521Tokenizer : public DwTokenizer {
  115. public:
  116.     DwRfc1521Tokenizer(const DwString& aStr);
  117.     DwRfc1521Tokenizer(const char* aCStr);
  118.     virtual ~DwRfc1521Tokenizer();
  119.     int Restart();
  120.     int operator ++ (); // prefix increment operator
  121. private:
  122.     DwRfc1521Tokenizer();
  123.     DwRfc1521Tokenizer(const DwRfc1521Tokenizer&);
  124.     void ParseToken();
  125.     void ParseAtom();
  126. };
  127.  
  128.  
  129. // DwTokenString allows us to build a DwString of tokens by concatenating
  130. // them.  This is not the normal string concatenation: the tokens are
  131. // assumed to have the same string rep, and the concatenated string shares
  132. // the rep.
  133.  
  134. class DW_EXPORT DwTokenString {
  135. public:
  136.     DwTokenString(const DwString&);
  137.     virtual ~DwTokenString();
  138.     const DwString& Tokens() const { return mTokens; }
  139.     void SetFirst(const DwTokenizer&);
  140.     void SetLast(const DwTokenizer&);
  141.     void ExtendTo(const DwTokenizer&);
  142.     // void Concatenate(const DwTokenizer&);
  143. protected:
  144.     const DwString mString;
  145.     DwString mTokens;
  146.     size_t mTokensStart;
  147.     size_t mTokensLength;
  148. };
  149.  
  150. #endif
  151.